package com.creating.mict.service.impl;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import com.creating.mict.dao.XtyDao;
import com.creating.mict.domain.XtyRecord;
import com.creating.mict.service.IXtyService;

public class XtyServiceImpl implements IXtyService {

	@Resource
	private XtyDao xtyDao;
	
	/**
	 * 
	* @Title: queryPatientXty 
	* @Description: 获取血糖仪签约患者及其测量结果
	* @param @param fbtime
	* @param @param fetime
	* @param @param fstate
	* @param @param fkeywords
	* @param @return 
	* @return List<XyjRecord>    返回类型 
	* @throws
	 */
	public List<XtyRecord> queryPatientXty(String loginName, String foffice, String fbtime, String fetime, String fstate, String fkeywords, String ftakeBack) {
		return xtyDao.queryPatientXty(loginName, foffice, fbtime, fetime, fstate, fkeywords, ftakeBack);
	}
	
	
	/**
	 * 
	* @Title: produceXtndToChart 
	* @Description: 获取血糖浓度测量数据变化曲线
	* @param @param hospitalName
	* @param @param fbh
	* @param @param fquery_str
	* @param @param ri
	* @param @param ftitle
	* @param @return 
	* @return String    返回类型 
	* @throws
	 */
	public String produceXtndToChart(String fbtime, String fetime, String fphone1, String fname, String fflag) {
		List<XtyRecord> list0 = new ArrayList<XtyRecord> ();
		List<XtyRecord> list1 = new ArrayList<XtyRecord> ();
		List<XtyRecord> list2 = new ArrayList<XtyRecord> ();
				
				list0 = xtyDao.produceXtyToChart0(fbtime, fetime, fphone1);
				list1 = xtyDao.produceXtyToChart1(fbtime, fetime, fphone1);
				list2 = xtyDao.produceXtyToChart2(fbtime, fetime, fphone1);
				
				//图表xml字符串
				StringBuffer xmlStr = new StringBuffer("");
				xmlStr.append("<chart bgColor='EEEEEE' showValues='0' labelDisplay='Rotate' slantLabels='1' baseFontSize='12' decimalPrecision='1' showNames='1' formatNumberScale='0' caption='" 
						+ fname + "血糖测量值变化曲线' baseFont='宋体' useRoundEdges='1' yAxisName='' canvasBorderThickness='1' numberPrefix='' " 
						+ " >");
				
				String value = "";
				XtyRecord xr = new XtyRecord();
				
				
				if(fflag.equals("0")) {
					//x轴
					xmlStr.append("<categories font='宋体' fontSize='12' fontColor='000000'>");
					for(int i = 0; i < list0.size(); i++) {
						xr = list0.get(i);
						if(xr != null) {
							value = xr.getFrdateStr().substring(5, 10);
						} else {
							value = "";
						}
						xmlStr.append("<category name='" + value + "' />"); 
					}
					xmlStr.append("</categories>");
					xmlStr.append("<dataset seriesName='餐前空腹血糖' color='1D8BD1' anchorBorderColor='1D8BD1' anchorBgColor='1D8BD1'>");
					for (int i = 0; i < list0.size(); i++) {
						xr = list0.get(i);
						if(xr != null) {
							value = xr.getFcqxt();
							if(value == null || value.equals("null") || value.equals("")) {
								xmlStr.append("<set value='0' />");
							} else {
								xmlStr.append("<set value='" + value + "' />");
							}
						} else {
							xmlStr.append("<set value='0' />");
						}
					}
					xmlStr.append("</dataset>");
				}
				
				if(fflag.equals("1")) {
					//x轴
					xmlStr.append("<categories font='宋体' fontSize='12' fontColor='000000'>");
					for(int i = 0; i < list1.size(); i++) {
						xr = list1.get(i);
						if(xr != null) {
							value = xr.getFrdateStr().substring(5, 10);
						} else {
							value = "";
						}
						xmlStr.append("<category name='" + value + "' />"); 
					}
					xmlStr.append("</categories>");
					
					xmlStr.append("<dataset seriesName='餐后一小时血糖' color='F1683C' anchorBorderColor='F1683C' anchorBgColor='F1683C'>");
					for (int i = 0; i < list1.size(); i++) {
						xr = list1.get(i);
						if(xr != null) {
							value = xr.getFchxt1();
							if(value == null || value.equals("null") || value.equals("")) {
								xmlStr.append("<set value='0' />");
							} else {
								xmlStr.append("<set value='" + value + "' />");
							}
						} else {
							xmlStr.append("<set value='0' />");
						}
					}
					xmlStr.append("</dataset>");
				}
				
				if(fflag.equals("2")) {
					//x轴
					xmlStr.append("<categories font='宋体' fontSize='12' fontColor='000000'>");
					for(int i = 0; i < list2.size(); i++) {
						xr = list2.get(i);
						if(xr != null) {
							value = xr.getFrdateStr().substring(5, 10);
						} else {
							value = "";
						}
						xmlStr.append("<category name='" + value + "' />"); 
					}
					xmlStr.append("</categories>");
					
					xmlStr.append("<dataset seriesName='餐后两小时血糖' color='2AD62A' anchorBorderColor='2AD62A' anchorBgColor='2AD62A'>");
					for (int i = 0; i < list2.size(); i++) {
						xr = list2.get(i);
						if(xr != null) {
							value = xr.getFchxt2();
							if(value == null || value.equals("null") || value.equals("")) {
								xmlStr.append("<set value='0' />");
							} else {
								xmlStr.append("<set value='" + value + "' />");
							}
						} else {
							xmlStr.append("<set value='0' />");
						}
					}
					xmlStr.append("</dataset>");
					
				}
				
				xmlStr.append("</chart>");
				return "" + xmlStr;
			} 
	
	    /**
	     * 
	    * @Title: saveXtyOfSetting 
	    * @Description: TODO(这里用一句话描述这个方法的作用) 
	    * @param @param fphone1
	    * @param @param fcqxt_low
	    * @param @param fcqxt_high
	    * @param @param fchxt1_low
	    * @param @param fchxt1_high
	    * @param @param fchxt2_low
	    * @param @param fchxt2_high
	    * @param @return 
	    * @return boolean    返回类型 
	    * @throws
	     */
		public boolean saveXtyOfSetting(String fphone1, String fcqxt_low, String fcqxt_high, String fchxt1_low, String fchxt1_high, String fchxt2_low, String fchxt2_high ) {
			return xtyDao.saveXtyOfSetting(fphone1, fcqxt_low, fcqxt_high, fchxt1_low, fchxt1_high,  fchxt2_low,  fchxt2_high);
		} 
		
		
		/**
		 * 
		* @Title: produceXtndToChartByPerson 
		* @Description: 获取血糖浓度测量数据变化曲线
		* @param @param hospitalName
		* @param @param fbh
		* @param @param fquery_str
		* @param @param ri
		* @param @param ftitle
		* @param @return 
		* @return String    返回类型 
		* @throws
		 */
		public String produceXtndToChartByPerson(String fphone1, String fstate, String fflag) {
			List<XtyRecord> list0 = new ArrayList<XtyRecord> ();
			List<XtyRecord> list1 = new ArrayList<XtyRecord> ();
			List<XtyRecord> list2 = new ArrayList<XtyRecord> ();
			
			int num = 0;
			if(fstate != null) { 
				num = Integer.parseInt(fstate);
			} else {
				num = 0;
			}
					
					list0 = xtyDao.produceXtyToChart0(fphone1, num);
					list1 = xtyDao.produceXtyToChart1(fphone1, num);
					list2 = xtyDao.produceXtyToChart2(fphone1, num);
					
					//图表xml字符串
					StringBuffer xmlStr = new StringBuffer("");
					xmlStr.append("<chart bgColor='EEEEEE' showValues='0' labelDisplay='Rotate' slantLabels='1' baseFontSize='12' decimalPrecision='1' showNames='1' formatNumberScale='0' caption='" 
							+ fphone1 + "血糖测量值变化曲线' baseFont='宋体' useRoundEdges='1' yAxisName='' canvasBorderThickness='1' numberPrefix='' " 
							+ " >");
					
					String value = "";
					XtyRecord xr = new XtyRecord();
					
					
					if(fflag.equals("0")) {
						//x轴
						xmlStr.append("<categories font='宋体' fontSize='12' fontColor='000000'>");
						for(int i = 0; i < list0.size(); i++) {
							xr = list0.get(i);
							if(xr != null) {
								value = xr.getFrdateStr().substring(5, 10);
							} else {
								value = "";
							}
							xmlStr.append("<category name='" + value + "' />"); 
						}
						xmlStr.append("</categories>");
						xmlStr.append("<dataset seriesName='餐前空腹血糖' color='1D8BD1' anchorBorderColor='1D8BD1' anchorBgColor='1D8BD1'>");
						for (int i = 0; i < list0.size(); i++) {
							xr = list0.get(i);
							if(xr != null) {
								value = xr.getFcqxt();
								if(value == null || value.equals("null") || value.equals("")) {
									xmlStr.append("<set value='0' />");
								} else {
									xmlStr.append("<set value='" + value + "' />");
								}
							} else {
								xmlStr.append("<set value='0' />");
							}
						}
						xmlStr.append("</dataset>");
					}
					
					if(fflag.equals("1")) {
						//x轴
						xmlStr.append("<categories font='宋体' fontSize='12' fontColor='000000'>");
						for(int i = 0; i < list1.size(); i++) {
							xr = list1.get(i);
							if(xr != null) {
								value = xr.getFrdateStr().substring(5, 10);
							} else {
								value = "";
							}
							xmlStr.append("<category name='" + value + "' />"); 
						}
						xmlStr.append("</categories>");
						
						xmlStr.append("<dataset seriesName='餐后一小时血糖' color='F1683C' anchorBorderColor='F1683C' anchorBgColor='F1683C'>");
						for (int i = 0; i < list1.size(); i++) {
							xr = list1.get(i);
							if(xr != null) {
								value = xr.getFchxt1();
								if(value == null || value.equals("null") || value.equals("")) {
									xmlStr.append("<set value='0' />");
								} else {
									xmlStr.append("<set value='" + value + "' />");
								}
							} else {
								xmlStr.append("<set value='0' />");
							}
						}
						xmlStr.append("</dataset>");
					}
					
					if(fflag.equals("2")) {
						//x轴
						xmlStr.append("<categories font='宋体' fontSize='12' fontColor='000000'>");
						for(int i = 0; i < list2.size(); i++) {
							xr = list2.get(i);
							if(xr != null) {
								value = xr.getFrdateStr().substring(5, 10);
							} else {
								value = "";
							}
							xmlStr.append("<category name='" + value + "' />"); 
						}
						xmlStr.append("</categories>");
						
						xmlStr.append("<dataset seriesName='餐后两小时血糖' color='2AD62A' anchorBorderColor='2AD62A' anchorBgColor='2AD62A'>");
						for (int i = 0; i < list2.size(); i++) {
							xr = list2.get(i);
							if(xr != null) {
								value = xr.getFchxt2();
								if(value == null || value.equals("null") || value.equals("")) {
									xmlStr.append("<set value='0' />");
								} else {
									xmlStr.append("<set value='" + value + "' />");
								}
							} else {
								xmlStr.append("<set value='0' />");
							}
						}
						xmlStr.append("</dataset>");
						
					}
					
					xmlStr.append("</chart>");
					return "" + xmlStr;
				} 
	

}
